package com.javabi.boardgame.game.monopoly.money;

public class Amount {

	private final Denomination denomination;
	private int amount;

	public Amount(Denomination denomination, int amount) {
		if (denomination == null) {
			throw new NullPointerException("denomination");
		}
		if (amount < 0) {
			throw new IllegalArgumentException("amount=" + amount);
		}
		this.denomination = denomination;
		this.amount = amount;
	}

	public Amount(Amount amount) {
		this.denomination = amount.getDenomination();
		this.amount = amount.get();
	}

	public Denomination getDenomination() {
		return denomination;
	}

	public int get() {
		return amount;
	}

	public void add(int amount) {
		if (amount < 1) {
			throw new IllegalArgumentException("amount=" + amount);
		}
		this.amount += amount;
	}

	public void remove(int amount) {
		if (amount < 1) {
			throw new IllegalArgumentException("amount=" + amount);
		}
		if (this.amount < amount) {
			throw new IllegalArgumentException(denomination + ": available=" + this.amount + ", remove=" + amount);
		}
		this.amount -= amount;
	}

	public int getCost() {
		return denomination.getCost(amount);
	}

	@Override
	public String toString() {
		return amount + "x" + denomination;
	}

}
